/**
 * creater:kunjiang.zhou@dhc.com.cn
 * create time:2017-05-31 20:16:07
 * describe:生成条码
 */
APP.controller('GenerateController', ['$scope', 'GenerateService',
    function ($scope, GenerateService) {

        /***变量声明***/
        $scope.printerData = {
            deviceNo: 'kdt1082610',
            key: '933b8',
            printContent: '',
            times: 1
        };

        /**
         * 初始化函数
         */
        $scope.init = function () {

        };

        /**
         * 打印方法
         */
        $scope.printerCode = function () {
            $scope.printerData = {
                deviceNo: 'kdt1082610',
                key: '933b8',
                printContent: '测试打印',
                times: 1
            };
            GenerateService.printer365($scope.printerData)
                .then(function (response) {
                    console.log('成功回调方法', response)
                }, function (response) {
                    console.log('失败回调方法', response)
                })
        };
        //查询打印机的状态
        $scope.printerState = function () {
            GenerateService.printerState()
                .success(function (response) {
                    console.log('成功回调方法', response)
                }).error(function (error) {
                console.log('成功回调方法', error);
            })
        };

        //蓝牙
        $scope.openBluetooth = function () {
            ble.enable(
                function () {
                    console.log("Bluetooth is enabled");
                },
                function () {
                    console.log("The user did *not* enable Bluetooth");
                }
            );
        };
        //蓝牙扫描
        $scope.bluetoothScan = function () {
            ble.scan([], 10, function (device) {//成功的回调函数
                    $scope.devices = [];
                    console.log('device', device);
                    $scope.devices.push(device);
                    $scope.$apply(function () {
                        $scope.devices.push(device);
                    });

                    return $scope.devices;
                }, function () {

                    //失败的回调函数

                }
            );
        };
        //连接其他蓝牙
        $scope.connectFun = function (device) {
            ble.connect(device.id, $scope.onConnected, $scope.onError);
        };


        /*
         $scope.init();//如果页面缓存则用此方法初始化
         */
        $scope.$on('$ionicView.beforeEnter', function () {
            $scope.init();
        });
    }
]);

/**
 * creater:kunjiang.zhou@dhc.com.cn
 * create time:2017-05-31 20:16:07
 * describe:生成条码
 */
APP.service('GenerateService', ['$http', 'UrlService', function ($http, UrlService) {
    /**
     * 打印机
     */
    this.printer365 = function (data) {
        var param = {};
        return $http.post('http://open.printcenter.cn:8080/addOrder', data);
    };
    //查询打印机的状态
    this.printerState = function () {
        var param = {
            deviceNo:'kdt1082610',
            key:'933b8'
        };
        return $http.post('http://open.printcenter.cn:8080/queryPrinterStatus', param);
    };

}]);
